import 'package:dio/dio.dart';

Future<Response<dynamic>> getUserByName(String name, Dio dio) async {
  Response<dynamic> response = await dio.get('/user/name/$name');
  return response;
}
Future<Response<dynamic>> getUserById(int id, Dio dio) async {
  Response<dynamic> response = await dio.get('/user/$id');
  return response;
}
Future<Response<dynamic>> getUserByAccount(String account, Dio dio) async {
  Response<dynamic> response = await dio.get('/user/account/$account');
  return response;
}
Future<Response> addUser(
    {String? name,
    String? password,
    String? email,
    String? phoneNumber,
      String?account,
    required Dio dio}) async {
  try {
    Response response = await dio.post('/user', data: {
      'name': name,
      'password': password,
      'email': email,
      'phoneNumber': phoneNumber,
      'account':account,
    });
    return response; // 返回成功的响应对象
  } catch (e) {
    // 处理请求失败或错误的情况
    print('Error: $e');
    throw Exception('Failed to add user: $e'); // 抛出异常
  }
}

Future<Response<dynamic>> updateUserById({
  required int id,
  String? name,
  String? password,
  String? email,
  String? phoneNumber,
  String? account,
  required Dio dio,
}) async {
  final Map<String, dynamic> formDataMap = {};

  if (name != null) {
    formDataMap['name'] = name;
  }
  if (password != null) {
    formDataMap['password'] = password;
  }
  if (email != null) {
    formDataMap['email'] = email;
  }
  if (phoneNumber != null) {
    formDataMap['phoneNumber'] = phoneNumber;
  }
  if (account != null) {
    formDataMap['account'] = account;
  }

  FormData formData = FormData.fromMap(formDataMap);

  try {
    Response<dynamic> response = await dio.put(
      '/user/$id',
      data: formData,
    );
    return response;
  } catch (e) {
    print('Error: $e');
    throw e;
  }
}
